package pri.black.garden.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Arrays;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pri.black.dogview.domain.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pri.black.garden.domain.Vegetables;
import pri.black.garden.service.IVegetablesService;
import pri.black.garden.biz.VegetablesBiz;

/**
 * gardenController
 * 
 * @author black
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/garden/vegetable")
public class VegetablesController
{
    @Autowired
    private VegetablesBiz vegetablesBiz;

    /**
     * 查询garden列表
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:list')")
    @GetMapping("/list")
    public R list(Page<Vegetables> vegetables,Vegetables query)
    {
        Page<Vegetables> list = vegetablesBiz.getService()
                .lambdaQuery()
                .like(query.getName() != null && !query.getName().isEmpty(),Vegetables::getName,query.getName())
                .orderByDesc(Vegetables::getId).page(vegetables);
        return R.ok(list);
    }

    @GetMapping("/list-all")
    public R listAll(){
        return R.ok(vegetablesBiz.getService().list());
    }

    /**
     * 获取garden详细信息
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id)
    {
        return R.ok(vegetablesBiz.getService().getById(id));
    }

    /**
     * 新增garden
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:add')")
    //@Log(title = "garden", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody Vegetables vegetables)
    {
        if (vegetables.getName().contains(",")) {
            String[] split = vegetables.getName().split(",");
            for (String name : split) {
                if (vegetablesBiz.hasName(name)) {
                    continue;
                }
                vegetables.setId(null);
                vegetables.setName(name.trim());
                vegetables.setCreateTime(LocalDateTime.now());
                vegetables.setDelFlag(0);
                vegetablesBiz.save(vegetables);
            }
            return R.ok();
        }else {
            vegetables.setDelFlag(0);
            vegetables.setCreateTime(LocalDateTime.now());
            return R.ok(vegetablesBiz.save(vegetables));
        }
    }

    /**
     * 修改garden
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:edit')")
    //@Log(title = "garden", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody Vegetables vegetables)
    {
        return R.ok(vegetablesBiz.update(vegetables));
    }

    /**
     * 删除garden
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:remove')")
    //@Log(title = "garden", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public R remove(@PathVariable("id") Long id)
    {
        return R.ok(vegetablesBiz.getService().removeById(id));
    }

}
